export default function (req, res) {
    const { body } = req;
    const { filters = {}, start = 1, limit = 10 } = body;
    
    // Define mock data sets
    const totalItems = 120; // Fixed total for consistent pagination mock
    const startIndex = (start - 1) * limit;
  
    const orderTypes = [1, 2]; // 1-体验订单, 2-充值订单
    const orderStatuses = [1, 2, 3, 4, 5, 6, 7]; // 1-待支付, 2-支付中, ..., 7-已取消
    const payTypes = [1, 2, 3, 4]; // 1-交付课时, 2-外部支付, 3-智牛币支付, 4-在线支付
    const paySubTypes = [1, 2, 3, 4, 5]; // 1-自有渠道, ..., 5-其他
    const grades = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "初一", "初二", "初三", "初四", "高一", "高二", "高三", "大一", "大二", "大三", "大四", "大四以上"];
    const updaters = [{ id: 101, name: "张三" }, { id: 102, name: "李四" }];
  
    const generateMockOrder = (index) => {
      const orderId = 80000 + index;
      const orderType = filters.types?.[0] || orderTypes[Math.floor(Math.random() * orderTypes.length)];
      const orderStatus = filters.statusList?.[0] || orderStatuses[Math.floor(Math.random() * orderStatuses.length)];
      const isRechargeOrder = orderType === 2;
      const studentPaidAmount = parseFloat((Math.random() * 2000 + 100).toFixed(2));
      const totalClasstime = isRechargeOrder ? parseFloat((Math.random() * 100 + 10).toFixed(1)) : 0;
      const usedClasstime = isRechargeOrder ? parseFloat((Math.random() * totalClasstime).toFixed(1)) : 0;
      const refundedClasstime = isRechargeOrder ? parseFloat((Math.random() * (totalClasstime - usedClasstime)).toFixed(1)) : 0;
      const unusedClasstime = isRechargeOrder ? parseFloat((totalClasstime - usedClasstime - refundedClasstime).toFixed(1)) : 0;
      const isRefundable = orderStatus === 3 || orderStatus === 6;
  
      // Use a fixed format for dates to match the API spec
      const formatDate = (date) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      };
  
      const createTime = new Date(Date.now() - Math.floor(Math.random() * 90) * 24 * 60 * 60 * 1000);
      const updateTime = new Date(createTime.getTime() + Math.floor(Math.random() * 30) * 24 * 60 * 60 * 1000);
  
      return {
        id: orderId,
        student: {
          id: Math.floor(Math.random() * 100000) + 1,
          name: `学员${Math.floor(Math.random() * 100) + 1}`,
          sn: `SN${Math.floor(Math.random() * 9000) + 1000}`,
          grade: grades[Math.floor(Math.random() * grades.length)],
          school: `模拟学校${Math.floor(Math.random() * 5) + 1}`,
          trainAccount: Math.random() < 0.5,
        },
        createTime: formatDate(createTime),
        orderNo: filters.orderNo || `ORD${Math.floor(Math.random() * 90000000) + 10000000}`,
        type: orderType,
        status: orderStatus,
        totalClasstime: totalClasstime,
        studentPaidAmount: studentPaidAmount,
        payType: payTypes[Math.floor(Math.random() * payTypes.length)],
        paySubType: paySubTypes[Math.floor(Math.random() * paySubTypes.length)],
        usedClasstime: usedClasstime,
        refundedClasstime: refundedClasstime,
        unusedClasstime: unusedClasstime,
        refundable: isRefundable,
        signer: `签约人${Math.floor(Math.random() * 10) + 1}`,
        updateTime: formatDate(updateTime),
        updater: updaters[Math.floor(Math.random() * updaters.length)],
      };
    };
  
    const mockDataList = Array.from({ length: totalItems }, (_, i) => generateMockOrder(i));
    
    // Implement filtering logic based on the request body
    const filteredList = mockDataList.filter(order => {
      // Filter by student ID
      if (filters.studentId && order.student.id !== filters.studentId) {
        return false;
      }
      // Filter by student keyword
      if (filters.student && filters.student.keyword && !(order.student.name.includes(filters.student.keyword) || order.student.sn.includes(filters.student.keyword))) {
        return false;
      }
      // Filter by order type
      if (filters.type && order.type !== filters.type) {
        return false;
      }
      if (filters.types && filters.types.length > 0 && !filters.types.includes(order.type)) {
        return false;
      }
      // Filter by order status
      if (filters.status && order.status !== filters.status) {
        return false;
      }
      if (filters.statusList && filters.statusList.length > 0 && !filters.statusList.includes(order.status)) {
        return false;
      }
      // Filter by order number
      if (filters.orderNo && !order.orderNo.includes(filters.orderNo)) {
        return false;
      }
      // Filter by creation time range
      if (filters.createTimeMin && new Date(order.createTime) < new Date(filters.createTimeMin)) {
        return false;
      }
      if (filters.createTimeMax && new Date(order.createTime) > new Date(filters.createTimeMax)) {
        return false;
      }
      return true;
    });
  
    // Apply pagination to the filtered list
    const paginatedList = filteredList.slice(startIndex, startIndex + limit);
  
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '成功！',
          data: {
            current: start,
            size: limit,
            total: filteredList.length,
            list: paginatedList,
            summary: {}, // Placeholder for summary
          },
          timestamp: Date.now(),
          requestId: 'requestId_' + Math.random().toString(36).substring(2, 15),
          signature: 'signature_' + Math.random().toString(36).substring(2, 15),
        });
      }, 100);
    });
  }